package com.wyzz.hr.action;

import java.math.BigDecimal;
import java.util.List;
import org.apache.axis.utils.StringUtils;
import org.apache.commons.beanutils.PropertyUtils;

import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.bo.IEmpSalaryAcctitemsBo;
import com.wyzz.hr.bo.IEmpSalaryAcctversionBo;
import com.wyzz.hr.bo.IJobgradeBO;
import com.wyzz.hr.bo.ISalaryconfBo;
import com.wyzz.hr.pojo.Empsalaryacctitems;
import com.wyzz.hr.pojo.Empsalaryacctversion;
import com.wyzz.hr.pojo.Empsalaryconfig;
import com.wyzz.hr.pojo.Jobgrade;
import com.wyzz.hr.spring.SpringBeanFactory;

public class DWRforSalaryCalc extends CompAction {

	public DWRforSalaryCalc() {
	}

	public Empsalaryconfig calculateSalary(String acctVersionId, String empId,
			String jobGradeId, Empsalaryconfig config) {
		if ("error".equalsIgnoreCase(DWRUtil.checkAuth("searchSalary",
				"execute")))
			return null;
		if (StringUtils.isEmpty(acctVersionId) || StringUtils.isEmpty(empId)
				|| StringUtils.isEmpty(jobGradeId))
			return null;
		Empsalaryconfig cfg = loadConfig(empId, null);
		if (cfg == null)
			return null;
		if (!(new Empsalaryacctversion(acctVersionId)).equals(cfg
				.getEscEsavId())) {
			IEmpSalaryAcctversionBo esvaBo = (IEmpSalaryAcctversionBo) SpringBeanFactory
					.getBean("empsalaryacctversionBo");
			Empsalaryacctversion version = esvaBo.loadObject(acctVersionId,
					new String[0]);
			cfg.setEscEsavId(version);
		}
		if (!(new Jobgrade(jobGradeId)).equals(cfg.getEscJobgrade())) {
			IJobgradeBO jobgradebo = (IJobgradeBO) SpringBeanFactory
					.getBean("jobgradeBO");
			Jobgrade jobgrade = jobgradebo.loadJobgrade(jobGradeId);
			cfg.setEscJobgrade(jobgrade);
		}
		IEmpSalaryAcctitemsBo esaiBo = (IEmpSalaryAcctitemsBo) SpringBeanFactory
				.getBean("empsalaryacctitemsBo");
		esaiBo.setAcctItemsConfig(new Empsalaryconfig[] { cfg });
		setConfigValues(config, cfg);
		ISalaryconfBo confBo = (ISalaryconfBo) getBean("salaryconfBo");
		confBo.interpretConfig(new Empsalaryconfig[] { cfg });
		cfg.setEscEsavId(null);
		cfg.setEmployee(null);
		cfg.setEscJobgrade(null);
		return cfg;
	}

	protected boolean setConfigValues(Empsalaryconfig source,
			Empsalaryconfig target) {
		if ((source == null) || (target == null))
			return false;
		List acctItems = target.getAcctItems();
		boolean result = true;
		for (int i = 0; i < acctItems.size(); ++i) {
			Integer isCalc = ((Empsalaryacctitems) acctItems.get(i))
					.getEsaiDataIsCalc();
			try {
				if (isCalc.intValue() != 2) {
					if (isCalc.intValue() == 1) {
						PropertyUtils.setProperty(target,
								"escColumn" + (i + 1), new BigDecimal("0.00"));
					} else {
						Object value = PropertyUtils.getProperty(source,
								"escColumn" + (i + 1));
						if (value == null)
							PropertyUtils.setProperty(target, "escColumn"
									+ (i + 1), new BigDecimal("0.00"));
						else
							PropertyUtils
									.setProperty(target, "escColumn" + (i + 1),
											((BigDecimal) value).setScale(2));
					}
				}
			} catch (Exception e) {
				System.out.println(e);
				result = false;
			}
		}
		return result;
	}

	private static final long serialVersionUID = 1L;
}
